package hejie.admin.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import publics.util.Page;

import hejie.admin.dao.EthnicDao;
import hejie.admin.dao.RightDao;
import hejie.admin.service.OtherService;

@Component("otherService")
public class OtherServiceImpl implements OtherService{

	RightDao rightDao;
	EthnicDao ethnicDao;
	 
	
	public RightDao getRightDao() {
		return rightDao;
	}
	
	@Resource(name="rightDao")
	public void setRightDao(RightDao rightDao) {
		this.rightDao = rightDao;
		System.out.println("rightDaoImpl inject in OtherServiceImpl");
	}
	public EthnicDao getEthnicDao() {
		return ethnicDao;
	}
	
	@Resource(name="ethnicDao")
	public void setEthnicDao(EthnicDao ethnicDao) {
		this.ethnicDao = ethnicDao;
		System.out.println("ethnicDaoImpl inject in OtherServiceImpl");
	}
	 
 
	public Page findEthnicByPage(Page page) {
//		String sql;			 
//		sql = "from Ethnic e ";
//		System.out.println("sql ="+sql);
//		int pageSize = page.getLimit();
//		int offset = page.getStart();
//		page.setRoot(ethnicDao.findByPage(sql,offset,pageSize));//把查询结果放入root中
//		page.setTotalProperty(ethnicDao.findByCount(sql));   
//		return page;
		StringBuffer sql = new StringBuffer();
		sql.append("from Ethnic e ");
		if(page.getConditions().size() > 0){//这里为模糊查询的 sql 语句拼接 请仔细体会
			sql.append(" where ");  
			for(int i = 0; i < page.getConditions().size(); ){
				sql.append("( e.name like '%").append(page.getConditions().get(i)).append("%' ) ");
				i++;
				if(i < page.getConditions().size()){ 
					sql.append(" and ");
				}
			}
		}
		System.out.println("sql ="+sql); 
		int pageSize = page.getLimit();
		int offset = page.getStart(); 
		page.setRoot(ethnicDao.findByPage(sql.toString(),offset,pageSize));//把查询结果放入root中
		page.setTotalProperty(ethnicDao.findByCount(sql.toString()));   //把数据总数放入totalProperty中		
		return page;
	}
	
	public Page findRightByPage(Page page) { 
		StringBuffer sql = new StringBuffer();
		sql.append("from Right r ");
		if(page.getConditions().size() > 0){//这里为模糊查询的 sql 语句拼接 请仔细体会
			sql.append(" where ");  
			for(int i = 0; i < page.getConditions().size(); ){
				sql.append("( r.name like '%").append(page.getConditions().get(i)).append("%' ) ");
				i++;
				if(i < page.getConditions().size()){ 
					sql.append(" and ");
				}
			}
		}
		System.out.println("进入findRightByPage "); 
		System.out.println("sql ="+sql); 
		int pageSize = page.getLimit();
		int offset = page.getStart(); 
		page.setRoot(ethnicDao.findByPage(sql.toString(),offset,pageSize));//把查询结果放入root中
		page.setTotalProperty(ethnicDao.findByCount(sql.toString()));   //把数据总数放入totalProperty中		
		return page;
	} 	
}
